package com.hong.chapter7;

public class SynchronizedRunnable implements  Runnable {
    private  int index = 1;

    //readonly
    private final  static  int MAX = 500;
    private  final  Object MONITOR = new Object();

    //this
    @Override
    public synchronized void run() {
        while (true){
            //1
            if(index >MAX)
                break;
            try {
                Thread.sleep(5);
            }catch (InterruptedException e){
                e.printStackTrace();
            }
            System.out.println(Thread.currentThread().getName()+"第："+(index++));
        }
    }

    private  synchronized boolean ticket(){
        //1.getField
        if(index>MAX)
            return true;
        try {
            Thread.sleep(5);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        //index++=>incdex =index+1
        //1.get Field index
        //2.index =index+1
        //3.put field index
        System.out.println(Thread.currentThread()+"的号码是"+(index++));
        return  false;
    }

}
